import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
 * Created with IntelliJ IDEA.
 * Description:模拟实现简单线程池
 * Date: 2024-08-26
 * Time: 22:43
 */

class MyThreadPool {
    private final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    public MyThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {
                try {
                    Runnable runnable = queue.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });

            thread.start();
        }
    }
}

public class ThreadPoolExample {
    MyThreadPool myThreadPool = new MyThreadPool(10);

}
